package Inter;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Josue
 */
public class Defun {
    String nombre;
    String var;
    String[] cuerpo;
    Aritmetica aritmetica;
    public Defun(String name, String args, String[] body){
        nombre = name;
        var = args;
        cuerpo = body;
        aritmetica = new Aritmetica();
    }
    public double operar(double val){
        double respu = 0;
        double respu1 =0;
        double respu2 = 0;
        double n1= 0;
        double n2= 0;
        //System.out.println(cuerpo[0]);
        if (!(cuerpo[0].length()==2)){
            
              //System.out.println("TRUE");
              //System.out.println(cuerpo[2]);
                cuerpo[2]= cuerpo[2].replace(')', 'a');
                String[] par1 = cuerpo[2].split("a");
                //System.out.println(par1[0]);
                if(cuerpo[0].endsWith("+")){
                    if(cuerpo[1].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double(cuerpo[1]);
                    }
                    
                    if(par1[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        n2 = new Double(par1[0]);
                    }
                    respu1 = aritmetica.sumar(n1, n2);
                }
                if((cuerpo[0].endsWith("-"))){
                    System.out.println("menos");
                    if(cuerpo[1].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double((cuerpo[1]));
                    }
                    if(par1[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        n2 = new Double(par1[0]);
                    }
                    respu1 = aritmetica.restar(n1, n2);
                }
                if(cuerpo[0].endsWith("*")){
                    if(cuerpo[1].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double(cuerpo[1]);
                    }
                    if(par1[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        n2 = new Double(par1[0]);
                    }
                    respu1 = aritmetica.multi(n1, n2);
                }
                if(cuerpo[0].endsWith("/")){
                    if(cuerpo[1].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double(cuerpo[1]);
                    }
                    if(par1[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        n2 = new Double(par1[0]);
                    }
                    respu1 = aritmetica.dividir(n1, n2);
                }
                //System.out.println(cuerpo[4]);
                cuerpo[4]= cuerpo[4].replace(')', 'g');
                String[] par2 = cuerpo[4].split("g");
                if(cuerpo[2].endsWith("+")){
                    if(cuerpo[3].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double(cuerpo[3]);
                    }
                    if(par2[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        n2 = new Double(par2[0]);
                    }
                    respu2 = aritmetica.sumar(n1, n2);
                }
               if(cuerpo[2].endsWith("-")){
                    if(cuerpo[3].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double(cuerpo[3]);
                    }
                    if(par2[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        n2 = new Double(par2[0]);
                    }
                    respu2 = aritmetica.restar(n1, n2);
                }
               if(cuerpo[2].endsWith("*")){
                    if(cuerpo[3].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double(cuerpo[3]);
                    }
                    if(par2[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        n2 = new Double(par2[0]);
                    }
                    respu2 = aritmetica.multi(n1, n2);
                }
               if(cuerpo[2].endsWith("/")){
                   System.out.println("div");
                    if(cuerpo[3].equals(var)){
                        n1 = val;
                    }
                    else{
                        n1 = new Double(cuerpo[3]);
                    }
                    if(par2[0].equals(var)){
                        n2 = val;
                    }
                    else{
                        //System.out.println(par2[0]);
                        n2 = new Double(par2[0]);
                    }
                    respu2 = aritmetica.dividir(n1, n2);
                }
        }
        else{
            if(cuerpo[1].equals(var)) respu1 = val;
            else respu1 = new Double(cuerpo[1]);
     
            cuerpo[2] = cuerpo[2].replace(')', 'g');
            String[] d = cuerpo[2].split("g");
            if(d[0].equals(var)) respu2 = val;
            else respu2 = new Double(d[0]);
                }
            if (cuerpo[0].substring(0,2).endsWith("*")){
                respu = aritmetica.multi(respu1, respu2);
            }
            if (cuerpo[0].substring(0,2).endsWith("+")){
                respu = aritmetica.sumar(respu1, respu2);
            }
            if (cuerpo[0].substring(0,2).endsWith("-")){
                respu = aritmetica.restar(respu1, respu2);
            }
            if (cuerpo[0].substring(0,2).endsWith("/")){
                respu = aritmetica.dividir(respu1, respu2);
            }
        
        //System.out.println(respu1);
        //System.out.println(respu2);
        return respu;
    }
}
